<?php
/*
产品列表
*/
class Con_product extends Acan{
	public $defaultAct='list';
	function __construct($ainfo=null){
		parent::__construct($ainfo=null);
		if(empty($this->id) || $this->id == '*'){$this->id = '0';}
		$this->model = Afun_db()->model(Acan_table_product);
		if($_POST){Afun_cache_rm('Aproduct');}//有数据库操作删除导航缓存
	}
	//--产品列表
	function Act_list(){
		if(empty($this->title)){$this->title = '产品列表';}
		$form_val_enabled = array('1'=>'是','2'=>'否');
		//分页函数
//		$con->where = "`pid`='$this->id'";
		$page->theurl = $this->Aactr.'-'.$this->id;
		$page->by = $this->model->key_name;
		$page->page = '10';
		$page->counts = $this->model->count($con);	//统计总数目
		$pages = Afun_apage($page);
		$con->select = "a.*,(SELECT SUM(s.`num`) FROM `".Acan_table_stock."` s WHERE a.`pid`=s.`pid`) as nums";
//		$con->join = "LEFT JOIN `".Acan_table_stock."` s ON a.`pid`=s.`pid`";
		$con->order = "a.enabled asc,a.look asc,a.$pages->orderby";
		$con->limit = $pages->limit;
		//分页结束
		$result = $this->model->findAll($con);
		foreach ($result as $key => $val) {
			if($_GET['search']){
				$val['name']=preg_replace("/($key)/i","<font color=red><b>\\1</b></font>",$val['name']);
			}
			$list[] = $val;
		}
		//获取父菜单信息
		$listp= $this->model->ofindPk($this->id);
		include $this->Atpl();
	}
	//批量更新
	function Act_save(){
		Afun_post_auth();
		foreach($_POST['save'] as $i=>$vals){
			$this->model->updatePk($vals,$vals['pid']);
		}
		echo '保存成功';
	//	Afun_refresh('保存成功','referrer');
	}
	//添加
	function Act_add(){
		Afun_post_auth();
		if($this->model->save($_POST['add'])){
			Afun_refresh('添加成功','referrer');
		}else{Afun_refresh('添加失败','referrer');}
	}
	//删除
	function Act_del(){
		Afun_post_auth();
		if($this->model->deletePk($this->id)){
			Afun_refresh('删除成功','referrer');
		}
	}
	//删除所选
	function Act_delall(){
		Afun_post_auth();
		foreach($_POST['chk'] as $key=>$val){
			if(empty($vala))
				$vala = "'$val'";
			else
				$vala .=",'$val'";
		}
		$this->model->delete($this->model->key_name." IN ($vala)");
		Afun_refresh("删除成功",'referrer');
	}
	
}

?>